<template>
  <div>
    <el-row class="row-bg" justify="space-between">
      <el-col :span="6">
        <TitleName
          :title="'实施记录'"
          icon="i-carbon-bookmark-filled"
          class="title"
      /></el-col>
      <el-col :span="12">
        <div class="text-center">
          <el-radio-group v-model="recordType" @change="changeRecord">
            <el-radio-button
              v-for="item in recordTypeOption"
              :key="item.id"
              :label="item.id"
              >{{ item.label }}</el-radio-button
            >
          </el-radio-group>
        </div>
      </el-col>
      <el-col :span="6" class="text-right">
        <el-button type="primary" plain @click="handleRecord"
          >新增</el-button
        ></el-col
      >
    </el-row>
    <div v-if="recordType == 'ssqk'">
      <SituationTable :queryParams="_queryParams" />
    </div>
    <div v-if="recordType == 'bgsq'">
      <ProjectChangeTable :queryParams="_queryParams" />
    </div>
    <div v-if="recordType == 'jxbg'">
      <ClosingReportTable :queryParams="_queryParams" />
    </div>
    <div v-if="recordType == 'ysdj'">
      <Acceptance :acceptanceTable="acceptanceTable" :showTitle="false" />
    </div>

    <!-- 选择新增实施记录表单弹窗 -->
    <el-dialog
      v-if="recordFormDialog.open"
      :title="recordFormDialog.title"
      v-model="recordFormDialog.open"
      width="80%"
      :close-on-click-modal="false"
    >
      <SituationForm
        v-if="recordType == 'ssqk'"
        :situationData="initForm"
        @processFun="processFun"
        @saveFun="saveFun"
      />
      <ProjectChangeForm
        v-if="recordType == 'bgsq'"
        :projectChangeData="initForm"
        @processFun="processFun"
        @saveFun="saveFun"
      />
      <ClosingReportForm
        v-if="recordType == 'jxbg'"
        :closingReportData="initForm"
        @processFun="processFun"
        @saveFun="saveFun"
      />
    </el-dialog>

    <!-- 验收登记 -->
    <CheckAndAccept
      :checkParam="checkParam"
      :baseDetail="_queryParams"
      @close="checkParam.visible = false"
      :contractTable="_queryParams.contractTable"
    />
  </div>
</template>

<script setup name="POverview">
import { getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import SituationTable from './situationTable.vue'
import ProjectChangeTable from './projectChangeTable.vue'
import ClosingReportTable from './closingReportTable.vue'
import Acceptance from '@/views/salesManage/projectManage/projectDetails/modules/acceptance'
import CheckAndAccept from '@/views/salesManage/projectManage/projectDetails/modules/checkAndAccept'
import SituationForm from '@/views/salesManage/projectManage/ImplementRecords/situationForm'
import ProjectChangeForm from '@/views/salesManage/projectManage/ImplementRecords/projectChangeForm'
import ClosingReportForm from '@/views/salesManage/projectManage/ImplementRecords/closingReportForm'
import { listRepaymentRecord } from '@/api/repaymentManage/repayment'
import * as Api from '@/api/projectManage/project'
const router = useRouter()
const { proxy } = getCurrentInstance()
// 接受父级传递的参数
const props = defineProps({
  queryParams: {
    required: true,
    type: Object || Array,
    default: () => {
      return {}
    }
  }
})
const _queryParams = ref({})
watch(
  () => props.queryParams,
  async () => {
    _queryParams.value = props.queryParams
  },
  {
    immediate: true,
    deep: true
  }
)
const acceptanceTable = ref([])
// 获取请求验收列表
const getAcceptance = async () => {
  const acceptance = await Api.getListByProjectId({
    project_id: _queryParams.value.projectId
  })
  acceptanceTable.value = acceptance.data
}
const changeRecord = val => {
  if (recordType.value == 'ysdj') {
    getAcceptance()
  }
}

const recordType = ref('ssqk')
const recordTypeOption = ref([
  {
    id: 'ssqk',
    label: '实施情况'
  },
  {
    id: 'bgsq',
    label: '变更申请'
  },
  {
    id: 'jxbg',
    label: '结项报告'
  },
  {
    id: 'ysdj',
    label: '验收登记'
  }
])
const recordTypeDialog = ref({
  title: '选择新增实施记录类型',
  open: false
})
const recordFormDialog = ref({
  title: computed(() => {
    return `项目${
      recordTypeOption.value.filter(item => item.id == recordType.value)[0]
        .label
    }`
  }),
  open: false
})
const initForm = ref({})
const checkParam = ref({}) //验收登记
const handleRecord = async () => {
  if (recordType.value == 'ysdj') {
    checkParam.value.visible = true
  } else {
    // 获取合同名称
    let contractName = _queryParams.value.contractTable
      .map(item => item.contractName)
      .toString()
    let contractNum = _queryParams.value.contractTable
      .map(item => item.contractNum)
      .toString()

    let repaymentList = []
    let repaymentPrice = 0
    if (recordType.value == 'jxbg') {
      // 获取回款列表
      let res = await listRepaymentRecord({
        projectId: _queryParams.value.projectId
      })
      repaymentList = res.rows
      repaymentList.forEach(item => {
        repaymentPrice += item.price
      })
    }
    recordTypeDialog.value.open = false
    initForm.value = {
      projectId: _queryParams.value.projectId,
      proName: _queryParams.value.proName,
      proNum: _queryParams.value.proNum,
      projectLeader:
        recordType.value == 'ssqk' ? _queryParams.value.principalId : undefined,
      projectLeaderName:
        recordType.value == 'ssqk' ? _queryParams.value.principal : undefined,
      manager:
        recordType.value == 'jxbg' ? _queryParams.value.principalId : undefined,
      managerName:
        recordType.value == 'jxbg' ? _queryParams.value.principal : undefined,
      customer:
        recordType.value == 'jxbg' ? _queryParams.value.customer : undefined,
      contractName: recordType.value == 'jxbg' ? contractName : undefined,
      contractNum: recordType.value == 'jxbg' ? contractNum : undefined,
      income:
        recordType.value == 'jxbg' ? _queryParams.value.proSum : undefined,
      repayment:
        recordType.value == 'jxbg' && repaymentPrice > 0
          ? repaymentPrice
          : undefined
    }
    recordFormDialog.value.open = true
  }
}
const processFun = val => {
  console.log('发起申请==》', val)
  recordFormDialog.value.open = false
  _queryParams.value = { ..._queryParams.value }
}
const saveFun = val => {
  console.log('保存==》', val)
  _queryParams.value = { ..._queryParams.value }
}
</script>

<style scoped lang="scss"></style>
